package com.ziqing.apeletnews;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.ziqing.apeletnews.base.BaseActivity;
import com.ziqing.apeletnews.baseMvp.BasePresenter;
import com.ziqing.apeletnews.fragment.ArticleFragment;
import com.ziqing.apeletnews.fragment.DiscoveryFragment;
import com.ziqing.apeletnews.fragment.HomeFragment;
import com.ziqing.apeletnews.fragment.LoginFragment;
import com.ziqing.apeletnews.fragment.PersonalFragment;
import com.ziqing.apeletnews.utils.NoScrollViewPager;
import com.ziqing.apeletnews.utils.SystemTypeUtil;



import butterknife.BindView;
import butterknife.ButterKnife;
import static com.blankj.utilcode.utils.ScreenUtils.getScreenWidth;

public class MainActivity extends BaseActivity {

    @BindView(R.id.home_view_pager)
    NoScrollViewPager homeViewPager;
    @BindView(R.id.home_tab_layout)
    TabLayout homeTabLayout;

    private String[] mTitles = new String[]{"首页", "文章", "","发现", "我的"};
    private int[] imgs = {R.drawable.icon_home,R.drawable.icon_article,R.drawable.ic_add_circle_black_24dp,R.drawable.icon_discovery,R.drawable.icon_account};

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initViews();

    }
    public boolean setStatusBarMode(boolean isDark) {
        Window window = getWindow();
        return SystemTypeUtil.setStatusBarLightMode(window, isDark);
    }

    private void initViews() {
        //Fragment页面加载
        homeViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (position == 1) {
                    return new ArticleFragment();
                } else if (position == 2) {
                    return new LoginFragment();
                } else if (position == 3) {
                    return new DiscoveryFragment();
                } else if (position == 4) {
                    return new PersonalFragment();
                }
                return new HomeFragment();
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }

        });
        //TabLayout绑定ViewPager
        homeTabLayout.setupWithViewPager(homeViewPager);
        //预加载全部
        homeViewPager.setOffscreenPageLimit(5);
        homeTabLayout.getTabAt(0).setCustomView(getTabView(0));
        homeTabLayout.getTabAt(1).setCustomView(getTabView(1));
        homeTabLayout.getTabAt(2).setCustomView(getTabView(2));
        homeTabLayout.getTabAt(3).setCustomView(getTabView(3));
        homeTabLayout.getTabAt(4).setCustomView(getTabView(4));
        homeTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == homeTabLayout.getTabAt(0)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_home_filling);
                    homeViewPager.setCurrentItem(0);
                } else if (tab == homeTabLayout.getTabAt(1)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_article_filling);
                    homeViewPager.setCurrentItem(1);
                }
//                } else if (tab == homeTabLayout.getTabAt(2)) {
//                    ((ImageView) tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_savemoney_press);
//                    ((TextView) tab.getCustomView().findViewById(R.id.tv_title)).setTextColor(getResources().getColor(R.color.colorAccent));
//                    homeViewPager.setCurrentItem(2);
//                }
                 else if (tab == homeTabLayout.getTabAt(3)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_discover_filling);
                    homeViewPager.setCurrentItem(3);
                }
                else if (tab == homeTabLayout.getTabAt(4)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_account_filling);
                    homeViewPager.setCurrentItem(4);
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab == homeTabLayout.getTabAt(0)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_home);
                } else if (tab == homeTabLayout.getTabAt(1)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_article);
                } else if (tab == homeTabLayout.getTabAt(2)) {

                } else if (tab == homeTabLayout.getTabAt(3)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_discovery);
                }
                else if (tab == homeTabLayout.getTabAt(4)) {
                    ((ImageView)tab.getCustomView().findViewById(R.id.iv_icon)).setImageResource(R.drawable.icon_account);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private View getTabView(int position) {

        View view = View.inflate(this, R.layout.tab_item, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        ImageView ivIcon = view.findViewById(R.id.iv_icon);

        tvTitle.setText(mTitles[position]);
        ivIcon.setImageResource(imgs[position]);

        if (position == 0) {
            tvTitle.setTextColor(getResources().getColor(R.color.colorLittleBlack));
            ivIcon.setImageResource(R.drawable.icon_home_filling);
        }else if(position == 2){
            int screenWidth = getScreenWidth(this); // 获取屏幕宽度
            ViewGroup.LayoutParams lp = ivIcon.getLayoutParams();
            lp.width = screenWidth;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            ivIcon.setLayoutParams(lp);
            ivIcon.setMaxWidth(screenWidth);
//            ivIcon.setMaxHeight(screenWidth * 5);
            tvTitle.setVisibility(View.GONE);
        }

        return view;
    }
}
